<template>
  <div class="doubao-app" :class="{ 'dark-mode': isDarkMode }">
    <!-- 顶部导航栏 -->
    <header class="app-header">
      <div class="logo-container">
        <div class="logo">豆</div>
        <h1 class="app-title">豆包智能助手</h1>
      </div>
      <div class="header-actions">
        <button class="theme-btn" @click="toggleTheme">
          <i :class="isDarkMode ? 'icon-moon' : 'icon-sun'"></i>
        </button>
        <button class="history-btn" @click="showHistory = !showHistory">
          <i class="icon-history"></i>
        </button>
      </div>
    </header>

    <!-- 主内容区 -->
    <main class="app-main">
      <!-- 聊天区域 -->
      <div class="chat-area">
        <div 
          ref="messagesContainer" 
          class="messages-container"
          v-if="messages.length > 0"
        >
          <div 
            class="message bot-message" 
            v-for="(msg, index) in messages" 
            :key="index"
            :class="{ 'has-error': msg.error }"
          >
            <div class="avatar">
              <div class="bot-avatar">豆</div>
            </div>
            <div class="message-content">
              <div class="text-content" v-html="msg.content"></div>
              <div class="message-time">{{ formatTime(msg.time) }}</div>
            </div>
          </div>
          <div 
            class="message user-message" 
            v-for="(msg, index) in userMessages" 
            :key="index"
          >
            <div class="avatar">
              <div class="user-avatar">我</div>
            </div>
            <div class="message-content">
              <div class="text-content" v-html="msg.content"></div>
              <div class="message-time">{{ formatTime(msg.time) }}</div>
            </div>
          </div>
        </div>

        <!-- 空状态提示 -->
        <div class="empty-state" v-else>
          <div class="empty-icon">🤖</div>
          <div class="empty-text">你好！我是豆包，有什么可以帮助你的吗？</div>
        </div>
      </div>

      <!-- 输入区域 -->
      <div class="input-area">
        <div class="input-toolbar">
          <button class="emoji-btn" @click="showEmoji = !showEmoji">
            <i class="icon-emoji"></i>
          </button>
          <button class="attachment-btn">
            <i class="icon-attachment"></i>
          </button>
        </div>
        <textarea 
          v-model="inputContent" 
          placeholder="请输入内容..." 
          @keydown.enter.prevent="sendMessage"
          @focus="showEmoji = false"
          class="chat-input"
        ></textarea>
        <button 
          @click="sendMessage" 
          class="send-btn"
          :class="{ 'sending': isSending }"
        >
          <i v-if="!isSending" class="icon-send"></i>
          <i v-else class="icon-loading"></i>
        </button>
      </div>
    </main>

    <!-- 历史记录侧边栏 -->
    <aside class="history-sidebar" v-show="showHistory">
      <div class="sidebar-header">
        <h2 class="sidebar-title">历史记录</h2>
        <button @click="showHistory = false" class="close-btn">
          <i class="icon-close"></i>
        </button>
      </div>
      <div class="history-search">
        <input 
          type="text" 
          v-model="searchQuery" 
          placeholder="搜索历史记录..." 
          class="search-input"
        >
      </div>
      <div class="history-list">
        <div 
          class="history-item" 
          v-for="(history, index) in filteredHistory" 
          :key="index"
          @click="selectHistory(history)"
          :class="{ 'active': selectedHistory === history }"
        >
          <div class="history-content">{{ truncate(history.content, 30) }}</div>
          <div class="history-time">{{ formatTime(history.time) }}</div>
        </div>
      </div>
    </aside>
  </div>
</template>

<script>
export default {
  name: 'DoubaoAssistant',
  data() {
    return {
      isDarkMode: localStorage.getItem('darkMode') === 'true',
      messages: [
        { 
          content: '你好！我是豆包，有什么可以帮助你的吗？', 
          time: new Date(),
          error: false
        }
      ],
      userMessages: [],
      inputContent: '',
      searchQuery: '',
      historyList: [
        { content: '如何使用Vue.js开发组件？', time: new Date() },
        { content: '今天北京的天气怎么样？', time: new Date() },
        { content: '推荐几部科幻电影', time: new Date() },
        { content: '前端性能优化的方法有哪些？', time: new Date() },
        { content: '人工智能的发展趋势', time: new Date() }
      ],
      selectedHistory: null,
      showHistory: false,
      showEmoji: false,
      isSending: false
    }
  },
  computed: {
    filteredHistory() {
      if (!this.searchQuery) return this.historyList;
      return this.historyList.filter(item => 
        item.content.toLowerCase().includes(this.searchQuery.toLowerCase())
      );
    }
  },
  methods: {
    sendMessage() {
      if (this.inputContent.trim() === '') return;
      
      this.isSending = true;
      const now = new Date();
      const userMsg = {
        content: this.inputContent,
        time: now
      };
      this.userMessages.push(userMsg);
      
      // 保存到历史记录
      this.historyList.unshift({
        content: this.inputContent,
        time: now
      });
      
      // 模拟豆包回复
      setTimeout(() => {
        let response = this.generateResponse(this.inputContent);
        this.messages.push({
          content: response,
          time: now,
          error: false
        });
        this.isSending = false;
        this.scrollToBottom();
      }, 1000);
      
      this.inputContent = '';
    },
    
    generateResponse(input) {
      const lowerInput = input.toLowerCase();
      if (lowerInput.includes('你好') || lowerInput.includes('您好')) {
        return '你好呀～很高兴为你服务！';
      } else if (lowerInput.includes('天气')) {
        return '当前天气晴朗，气温25℃，适合外出活动哦～';
      } else if (lowerInput.includes('电影')) {
        return '推荐《星际穿越》《银翼杀手2049》《黑客帝国》，都是非常经典的科幻作品～';
      } else if (lowerInput.includes('vue')) {
        return 'Vue.js开发组件需要先了解单文件组件结构，建议从官方文档学习基础用法～';
      } else {
        return `你刚刚说："${this.inputContent}"，我已经收到啦～`;
      }
    },
    
    selectHistory(history) {
      this.inputContent = history.content;
      this.selectedHistory = history;
    },
    
    toggleTheme() {
      this.isDarkMode = !this.isDarkMode;
      localStorage.setItem('darkMode', this.isDarkMode);
    },
    
    truncate(text, length) {
      if (text.length <= length) return text;
      return text.substring(0, length) + '...';
    },
    
    formatTime(date) {
      const hours = date.getHours().toString().padStart(2, '0');
      const minutes = date.getMinutes().toString().padStart(2, '0');
      return `${hours}:${minutes}`;
    },
    
    scrollToBottom() {
      this.$nextTick(() => {
        const container = this.$refs.messagesContainer;
        if (container) container.scrollTop = container.scrollHeight;
      });
    }
  },
  mounted() {
    this.scrollToBottom();
  }
}
</script>

<style scoped>
/* 基础样式 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
}

body {
  height: 100vh;
  overflow: hidden;
  background-color: #f5f7fa;
  transition: background-color 0.3s ease;
}

.dark-mode body {
  background-color: #121212;
}

/* 应用整体布局 */
.doubao-app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

/* 顶部导航栏 */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  height: 60px;
  background-color: #fff;
  border-bottom: 1px solid #e5e5e5;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  position: relative;
  z-index: 10;
  transition: all 0.3s ease;
}

.dark-mode .app-header {
  background-color: #1e1e1e;
  border-bottom: 1px solid #333;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

.logo-container {
  display: flex;
  align-items: center;
}

.logo {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background-color: #42b983;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  font-weight: bold;
  margin-right: 12px;
}

.app-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  transition: color 0.3s ease;
}

.dark-mode .app-title {
  color: #e0e0e0;
}

.header-actions {
  display: flex;
  align-items: center;
}

.theme-btn, .history-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  margin-left: 10px;
  transition: all 0.3s ease;
  background: none;
  border: none;
  color: #666;
}

.dark-mode .theme-btn, .dark-mode .history-btn {
  color: #999;
}

.theme-btn:hover, .history-btn:hover {
  background-color: #f0f0f0;
}

.dark-mode .theme-btn:hover, .dark-mode .history-btn:hover {
  background-color: #2d2d2d;
}

/* 主内容区 */
.app-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* 聊天区域 */
.chat-area {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  transition: all 0.3s ease;
}

.dark-mode .chat-area {
  background-color: #1e1e1e;
}

.messages-container {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* 消息样式 */
.message {
  display: flex;
  align-items: flex-start;
  max-width: 100%;
}

.bot-message {
  align-self: flex-start;
}

.user-message {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.avatar {
  margin-top: 4px;
  margin-right: 12px;
}

.bot-avatar, .user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
}

.bot-avatar {
  background-color: #42b983;
  color: #fff;
}

.user-avatar {
  background-color: #4285f4;
  color: #fff;
}

.message-content {
  position: relative;
  max-width: 70%;
}

.text-content {
  padding: 12px 16px;
  border-radius: 18px 18px 18px 4px;
  background-color: #fff;
  color: #333;
  font-size: 16px;
  line-height: 1.5;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
  position: relative;
  transition: all 0.3s ease;
}

.dark-mode .text-content {
  background-color: #2d2d2d;
  color: #e0e0e0;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}

.user-message .message-content .text-content {
  border-radius: 18px 18px 4px 18px;
  background-color: #4285f4;
  color: #fff;
}

.dark-mode .user-message .message-content .text-content {
  background-color: #3478f6;
}

.message-content .text-content p {
  margin-bottom: 8px;
}

.message-content .text-content p:last-child {
  margin-bottom: 0;
}

.message-time {
  margin-top: 6px;
  font-size: 12px;
  color: #999;
  text-align: right;
  transition: color 0.3s ease;
}

.dark-mode .message-time {
  color: #999;
}

.user-message .message-content .message-time {
  text-align: left;
}

/* 空状态提示 */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: #999;
}

.dark-mode .empty-state {
  color: #666;
}

.empty-icon {
  font-size: 60px;
  margin-bottom: 16px;
}

.empty-text {
  font-size: 16px;
  text-align: center;
  line-height: 1.5;
}

/* 输入区域 */
.input-area {
  padding: 12px 20px;
  border-top: 1px solid #e5e5e5;
  background-color: #fff;
  transition: all 0.3s ease;
}

.dark-mode .input-area {
  border-top: 1px solid #333;
  background-color: #1e1e1e;
}

.input-toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 8px;
}

.emoji-btn, .attachment-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  background: none;
  border: none;
  color: #666;
  transition: all 0.3s ease;
}

.dark-mode .emoji-btn, .dark-mode .attachment-btn {
  color: #999;
}

.emoji-btn:hover, .attachment-btn:hover {
  background-color: #f0f0f0;
}

.dark-mode .emoji-btn:hover, .dark-mode .attachment-btn:hover {
  background-color: #2d2d2d;
}

.chat-input {
  width: 100%;
  height: 60px;
  padding: 12px 16px;
  border: 1px solid #e5e5e5;
  border-radius: 30px;
  font-size: 16px;
  line-height: 1.5;
  background-color: #f5f7fa;
}